﻿using C_sports_meet.ServiceReference2;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace C_sports_meet.Manager
{
    /// <summary>
    /// MSubject.xaml 的交互逻辑
    /// </summary>
    public partial class MEvent : Page, IService1Callback
    {
        //
        private Service1Client client; // 客户端代理类，用于客户端调用服务端方法
        private InstanceContext context; // 让服务端通过InstanceContext知道通信的是哪个客户端对象
        //
        public MEvent()
        {
            InitializeComponent();
            context = new InstanceContext(this); // 获取当前InstanceContext
            client = new Service1Client(context); // 根据InstanceContext创建客户端代理类
            
            Loaded += MEvent_Loaded;
            //DataGrid1.CellEditEnding += DataGrid1_CellEditEnding;
        }

        private async void MEvent_Loaded(object sender, RoutedEventArgs e)
        {
            await client.EventSelectAllAsync();
        }

        //private static List<string> editedColumns = new List<string>();
        //private void DataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        //{
        //    foreach (var cellInfo in DataGrid1.SelectedCells)
        //    {
        //        if (!editedColumns.Contains(cellInfo.Column.SortMemberPath))
        //        {
        //            editedColumns.Add(cellInfo.Column.SortMemberPath);
        //        }
        //    }
        //}

        private async void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            DataRowView selectedItem;
            try
            {
                selectedItem = (DataRowView)DataGrid1.SelectedItem;
            }
            catch
            {
                return;
            }
            int len = selectedItem.Row.Table.Columns.Count;
            string[] selectedItems = new string[len];
            for (int i = 0; i < len; i++)
                selectedItems[i] = selectedItem.Row[i].ToString();

            await client.EventUpdateAsync(selectedItems);
        }

        private async void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DataRowView selectedItem;
            try
            {
                selectedItem = (DataRowView)DataGrid1.SelectedItem;
            }
            catch
            {
                return;
            }

            string fno = selectedItem.Row[0].ToString();

            await client.EventDeleteAsync(fno);
        }

        private async void btnInsert_Click(object sender, RoutedEventArgs e)
        {
            DataRowView selectedItem;
            try
            {
                selectedItem = (DataRowView)DataGrid1.SelectedItem;
            }
            catch
            {
                return;
            }
            int len = selectedItem.Row.Table.Columns.Count;
            string[] selectedItems = new string[len];
            for (int i = 0; i < len; i++)
                selectedItems[i] = selectedItem.Row[i].ToString();

            await client.EventInsertAsync(selectedItems);
        }

        private async void btnFind_Click(object sender, RoutedEventArgs e)
        {
            string keyword = inputKw.Text;
            await client.EventSelectKeywordAsync(keyword);
        }

        private async void btnRefresh_Click(object sender, RoutedEventArgs e)
        {
            Task select = client.EventSelectAllAsync();
            await select; // 异步调用服务端方法，防止界面阻塞
        }

        public void ShowResults(DataTable dt, int res) // 实现回调接口，在客户端的操作，但是服务端调用
        {
            if (res == 0)
            {
                MessageBox.Show("操作失败");
            }
            else
            {
                MessageBox.Show("操作成功");
                DataGrid1.ItemsSource = dt.DefaultView;
            }
        }
    }
}
